package jackson.src.day04;

import java.util.StringJoiner;

/*
 * Author: JacksonCoder
 * FileName: StringJoiner
 * Version: 1.0.0
 */
public class StringJoinersDemo {
    public static void main(String[] args) {

        String[] names = {"张三","李四","王五","赵六"};
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("hello ");
        for(String name: names){
            stringBuilder.append(name).append(", ");
        }
        // end  delete   ","
        stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length());  //  delete(int start, int end)
        stringBuilder.append("!");
        System.out.println(stringBuilder.toString());  // hello 张三, 李四, 王五, 赵六!

        String[] man = {"Bob", "Alice", "Grace"};
        StringJoiner sj = new StringJoiner(", " , "hello ", "!");
//        sj.add("hello ");
        for(String m: man){
            sj.add(m);
        }
        System.out.println(sj.toString());   //  hello Bob, Alice, Grace!        Bob, Alice, Grace


        String[] p = {"alex","egon","wusir"};
        String s = String.join(", ", p);
        System.out.println(s);  //alex, egon, wusir

        System.out.println("=========================");
        String[] fields = {"name","position","salary"};
        String table = "employee";
        String select = buildSelectSql(table, fields);
        System.out.println(select);
        System.out.println("SELECT name, position, salary FROM employee".equals(select) ? "测试成功" : "测试失败");
    }


    static String buildSelectSql(String table, String[] fields){
        StringJoiner stj = new StringJoiner(", ","SELECT ",  " FROM " + table);
        for(String str: fields){
            stj.add(str);
        }
//        stj.add(table);
        return stj.toString();
    }
}

